package com.base.bean;

import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.base.domain.Socio;
import com.base.security.NavigationPages;
import com.base.service.SocioService;

@Component
@ManagedBean
@ViewScoped
public class UserBean {

	private Socio socio;
	
	private SocioDataModel socioDataModel;
	
	private List<Socio> socios;
	
	private List<Socio> selected;
	
	private Socio selectedSocio;
	
	@Autowired
	private SocioService socioService;

	public void init() {
		Socio socio = new Socio();
		
		socio.setActive(Boolean.TRUE);
		setSocio(socio);
	}
	
	public void loadSocios(){
		setSocios(socioService.getAll());

		socioDataModel = new SocioDataModel(getSocios());
	}

	public String guardar() {
		Socio socio = getSocio();
		socioService.save(socio);
		loadSocios();
		return NavigationPages.LISTA_SOCIOS;
	}
	
	public void eliminar(Integer id) {
		socioService.deleteById(id);
		
		loadSocios();
	}
	
	public String editar(Integer id) {
		Socio socio = socioService.get(id);		
		setSocio(socio);
		return NavigationPages.ALTA_EDICION_SOCIO;
	}
	
	public void eliminarSeleccionados() {
		socioService.delete(getSelected());
	
		setSelected(null);
		loadSocios();
	}
	
	public String cancelar() {
		return NavigationPages.INDEX_PAGE;
	}

	public Socio getSocio() {
		return socio;
	}

	public void setSocio(Socio socio) {
		this.socio = socio;
	}

	public List<Socio> getSocios() {
		return socios;
	}

	public void setSocios(List<Socio> socios) {
		this.socios = socios;
	}

	public List<Socio> getSelected() {
		return selected;
	}

	public void setSelected(List<Socio> selected) {
		this.selected = selected;
	}

	public SocioDataModel getSocioDataModel() {
		return socioDataModel;
	}

	public void setSocioDataModel(SocioDataModel socioDataModel) {
		this.socioDataModel = socioDataModel;
	}

	public Socio getSelectedSocio() {
		return selectedSocio;
	}

	public void setSelectedSocio(Socio selectedSocio) {
		this.selectedSocio = selectedSocio;
	}
	
	
		
}